using System;
using System.Collections.Generic;
using Reflector.CodeModel;

namespace LanguageWriterHelper
{
    public class LanguageWriter : ILanguageWriter
    {
        IFormatter formatter;
        ILanguageWriterConfiguration configuration;
        Dictionary<string, bool> visibilityConfiguration;
        VisitorManager manager;
        public LanguageWriter(IFormatter formatter, ILanguageWriterConfiguration configuration)
        {
            this.formatter = formatter;
            this.configuration = configuration;

            visibilityConfiguration = new Dictionary<string, bool>();
            manager = new VisitorManager(formatter, configuration, visibilityConfiguration);

            InitConfiguration();
        }

        private void InitConfiguration()
        {
            visibilityConfiguration.Add("only-assembly", false);
            visibilityConfiguration.Add("only-assemblyref", false);
            visibilityConfiguration.Add("only-eventdecl", false);
            visibilityConfiguration.Add("only-fielddecl", false);
            visibilityConfiguration.Add("only-methoddecl", false);
            visibilityConfiguration.Add("only-typedecl", false);
            visibilityConfiguration.Add("only-module", false);
            visibilityConfiguration.Add("only-moduleref", false);
        }

        public void WriteAssembly(IAssembly value)
        {
            visibilityConfiguration["only-assembly"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-assembly"] = false;
        }

        public void WriteAssemblyReference(IAssemblyReference value)
        {
            visibilityConfiguration["only-assemblyref"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-assemblyref"] = false;
        }

        public void WriteEventDeclaration(IEventDeclaration value)
        {
            visibilityConfiguration["only-eventdecl"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-eventdecl"] = false;
        }
             
        public void WriteFieldDeclaration(IFieldDeclaration value)
        {
            visibilityConfiguration["only-fielddecl"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-fielddecl"] = false;
        }

        public void WriteMethodDeclaration(IMethodDeclaration value)
        {
            visibilityConfiguration["only-methoddecl"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-methoddecl"] = false;
        }

        public void WriteModule(IModule value)
        {
            visibilityConfiguration["only-module"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-module"] = false;
        }

        public void WriteModuleReference(IModuleReference value)
        {
            visibilityConfiguration["only-moduleref"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-moduleref"] = false;
        }

        public void WriteNamespace(INamespace value)
        {
            visibilityConfiguration["only-namespace"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-namespace"] = false;
        }

        public void WritePropertyDeclaration(IPropertyDeclaration value)
        {
            visibilityConfiguration["only-propertydecl"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-propertydecl"] = false;
        }

        public void WriteResource(IResource value)
        {
            visibilityConfiguration["only-resource"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-resource"] = false;
        }

        public void WriteTypeDeclaration(ITypeDeclaration value)
        {
            visibilityConfiguration["only-typedecl"] = true;
            manager.MetadataVisitor.Visit(value);
            visibilityConfiguration["only-typedecl"] = false;
        }

        public void WriteStatement(IStatement value)
        {
            manager.StatementVisitor.Visit(value);
        }
                
        public void WriteExpression(IExpression value)
        {
            manager.ExpressionVisitor.Visit(value);
        }

    }
}
